Reconnect: never strand the state machine without a pending connect intent - #5
Open
loopkitdev wants to merge 2 commits into
Open
Reconnect: never strand the state machine without a pending connect intent#5loopkitdev wants to merge 2 commits into
loopkitdev wants to merge 2 commits into
Conversation
…races Two ways the reconnect state machine could strand itself "disconnected with nothing pending" (no monitor, no in-flight attempt, no CB connect intent to wake the app when the sensor returns), causing outages until a manual foreground: 1. scheduleReconnect re-armed the connect intent whenever the peripheral wasn't .connected/.connecting -- which included .disconnecting. Issuing central.connect while a cancelConnection is still in flight (e.g. the close-on-CCCD-failure path) leaves the peripheral in a phantom .connecting state that never fires didConnect and suppresses every later re-arm. Only request connect from .disconnected; wait for the didDisconnect otherwise. 2. After a failed attempt, the continuation cancelled the peripheral whenever it was .connected OR .connecting. But .connecting is normally the intent the preceding didDisconnect just re-armed, and cancelling a .connecting peripheral fires no terminal callback, silently dropping it. Only drop a fully .connected stale link (which does fire didDisconnect); leave .connecting/.disconnecting alone (a CB callback is coming) and re-arm from .disconnected. Field logs: 2026-07-25 15:53 (phantom .connecting, ~17-hour outage) and 2026-07-26 16:40 (cancelled the re-armed intent, ~6-min outage).
refreshPostAuthNotifications isn't cancellation-aware, so a monitor stopped mid-refresh runs to completion and then executes its close path -- cancelConnection on session.peripheral (same identifier as a newer attempt's peripheral) plus firing the disconnect handler -- derailing the live reconnect. Check Task.isCancelled after the refresh and return before touching the link. Field log 2026-07-25 15:53: a refresh started at :37, the monitor was stopped at :47, the refresh failed at :53 and dropped the fresh attempt mid-connect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two recent multi-hour CGM outages traced to the reconnect state machine stranding itself "disconnected with nothing pending" — no monitor, no in-flight attempt, and no CoreBluetooth connect intent to wake the app when the sensor returns — recovering only on a manual foreground. Both are races around the peripheral's transient
.connecting/.disconnectingstates.Fixes
1.
scheduleReconnect— onlyrequestConnectfrom.disconnected(was: anything except.connected/.connecting).Issuing
central.connectwhile acancelConnectionis still in flight (e.g. the close-on-CCCD-failure path drops the link and immediately reschedules) races the pending teardown. iOS leaves the peripheral in a phantom.connectingstate that never firesdidConnect, and that phantom state then suppresses every legitimate re-arm. Wait for thedidDisconnect; itsscheduleReconnectre-arms cleanly from.disconnected.Field log 2026-07-25 15:53 — ~17-hour outage.
2. Failed-attempt continuation — don't cancel a
.connectingperipheral (was: cancel on.connectedor.connecting)..connectingis normally the intent the precedingdidDisconnectjust re-armed. Cancelling a.connectingperipheral fires no terminal callback, so it silently drops that intent and strands us. Now it only drops a fully.connectedstale link (which does firedidDisconnect), leaves.connecting/.disconnectingalone (a CB callback is guaranteed to come and route back toscheduleReconnect), and re-arms only from.disconnected.Field log 2026-07-26 16:40 — the
didDisconnectre-armed the connect (requesting connect (peripheral state=0)), then this continuation cancelled it 1ms later; ~6-min outage.3. Sensor monitor bails if superseded during the CCCD refresh.
refreshPostAuthNotificationsisn't cancellation-aware, so a monitor stopped mid-refresh runs to completion and then executes its close path —cancelConnectiononsession.peripheral(same identifier as a newer attempt's peripheral) plus firing the disconnect handler — derailing the live reconnect. ATask.isCancelledcheck after the refresh returns before touching the link.Field log 2026-07-25 15:53 — a refresh started at :37, monitor stopped at :47, refresh failed at :53 and dropped the fresh attempt mid-connect.
Testing
Builds clean (LoopWorkspace scheme, device); installed and field-testing. The invariant to watch on recovery: a
requesting connect … (peripheral state=0)afterdidDisconnect, with nocancelConnectionimmediately following it.